package pl.alipiec.stockview.master;

import java.util.Random;

/**
 * Klasa-kontener na dane kursu akcji
 * 
 * @author Aleksandra Lipiec
 *
 */
public class StockData {
	
	public double min52weeks; // minimum 52 tygodni
	public double max52weeks; // maximum 52 tygodni
	public String time; // czas
	public String name; // nazwa
	public double rating; // kurs nominalnie
	public double changePercent; // zmiana(%)
	public double changePLN; // zmiana nominalnie
	public double ratingRef; // kurs odniesienia
	public double ratingOpen; // kurs otwarcie
	public double ratingMin; // kurs minimalny
	public double ratingMax; // kurs maksymalny
	public int turnoverUnit; // obroty wolumen
	public double turnover; // obroty wartość
	
	public StockData(String stockCSV) {
		
		Random rand = new Random();
		
		stockCSV = stockCSV.replace(',', '.');
		stockCSV = stockCSV.replaceAll(" ", "");
		String[] parts = stockCSV.split(";");

		System.out.println(stockCSV);

		min52weeks = parseDoubleElement(parts[0]);
		max52weeks = parseDoubleElement(parts[1]);
		time = parts[2];
		name = parts[3];
		rating = parseDoubleElement(parts[4]);
		changePercent = parseDoubleElement(parts[5]);
		changePLN = parseDoubleElement(parts[6]);
		ratingRef = parseDoubleElement(parts[7]);
		ratingOpen = parseDoubleElement(parts[8]);
		ratingMin = parseDoubleElement(parts[9]);
		ratingMax = parseDoubleElement(parts[10]);
		turnoverUnit = parseIntElement(parts[11]);
		turnover = parseDoubleElement(parts[12]);
		
		//randomizer
		if(rand.nextInt(4) % 4 == 0){
			double change = rand.nextInt(500)/(double)100 - 2.5;
			rating *= 1 + change/100;
			changePercent += change;
			changePLN *= 1 + change/100;
		}
		
	}
	
	private double parseDoubleElement(String str) {
		if(str.equals("---"))
			return 0.0;
		return Double.parseDouble(str);
	}
	
	private int parseIntElement(String str) {
		if(str.equals("---"))
			return 0;
		return Integer.parseInt(str);
	}

}
